/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.com.claro.administracionSolicitudes.beans;

import co.com.claro.administracionSolicitudes.entity.Usuario;
import co.com.claro.administracionSolicitudes.seguridad.EJBSeguridadLocal;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Mike
 */
@ManagedBean
@ViewScoped
public class LoginBean {
    
    @EJB
    private EJBSeguridadLocal seguridadLocal;

    @ManagedProperty(value = "#{usuarioSessionBean}")
    private UsuarioSessionBean sesionBean;

    private String username;
    private String password;

    /**
     * Creates a new instance of LoginBean
     */
    public LoginBean() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().
                getExternalContext().getSession(false);

        if (session != null) {
            session.invalidate();
        }
    }

    @PostConstruct
    public void inicializarValores() {

    }

    public String login() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().
                getExternalContext().getRequest();
//        StringBuilder navegacion = new StringBuilder();
        try {
            //Login via request
            request.login(username, password);

            Usuario usuario = seguridadLocal.findUsuarioActivoPorUsernameExacto(username);

            usuario.setUsuario(username);
            sesionBean.setUsuario(usuario);

            return "/Solicitante/inicio.jsf?faces-redirect=true";
        } catch (ServletException e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, e.getMessage(), null));
            return "error.jsf?faces-redirect=true";
        }
    }

    public void logout() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().
                getExternalContext().getSession(false);

        if (session != null) {
            session.invalidate();
        }

        FacesContext.getCurrentInstance().getApplication().getNavigationHandler().handleNavigation(FacesContext.getCurrentInstance(), null, "/login.xhtml?faces-redirect=true");
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public UsuarioSessionBean getSesionBean() {
        return sesionBean;
    }

    public void setSesionBean(UsuarioSessionBean sesionBean) {
        this.sesionBean = sesionBean;
    }

}
